<!--
  @Descripttion:：相似职位推荐模块
  @Date：2022年12月21日 12:30:00
-->
<template>
  <section class="same-job-com" v-if="dataList && dataList.length">
    <div class="title">相似职位推荐</div>
    <jobCard class="t_itemBox" :dataList="dataList"></jobCard>
    <div class="itemBox">
      <!-- <div class="row" v-loading="loadingList[index]" > -->
      <div v-if="dataList.length <= 0">
        <el-empty description="空空如也"></el-empty>
      </div>
      <div class="swiper-box" v-else>
        <div class="swiper" id="sameJob">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="(item, i) in dataList" :key="i">
              <div class="card-item" @click="linkTo(item.id)">
                <p class="item-title">{{ item.jobName }}</p>
                <p class="item-price">
                  {{ item.lowestSalary }}-{{ item.highestSalary }}元/天
                </p>
                <p class="item-address">{{ item.city }}</p>
                <div class="item-msg">
                  <span class="item-text">{{ item.practicePeriod }}个月</span>
                  <span class="line"></span>
                  <span class="item-text">{{ item.workPeriod }}</span>
                  <span class="line"></span>
                  <span class="item-text">{{ item.education }}</span>
                  <span class="line" v-if="item.identify"></span>
                  <span class="item-text" v-if="item.identify">{{
                    ["在校", "应届"][item.identify]
                  }}</span>
                </div>
                <div class="item-bottom">
                  <img v-if="!item.companyLogo" src="@/assets/images/search-icon.png" alt="" class="bottom-img" />
                  <img v-if="item.companyLogo" :src="item.companyLogo" alt="" class="bottom-img" />
                  <span class="bottom-title">{{
                    item.companyShortName || item.companyName
                  }}</span>
                  <span class="line"></span>
                  <span class="bottom-text">{{ item.industry }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div v-if="dataList.length > 0" class="steps left" @click="slidePrev()">
        <img src="../../../assets/images/hire/left2.png" />
      </div>
      <div v-if="dataList.length > 0" class="steps right" @click="slideNext()">
        <img src="../../../assets/images/hire/right2.png" />
      </div>
    </div>
  </section>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
import jobCard from '../../companyAndJobList/components/jobCard.vue'
import { getJobListOld } from "../../../request/api";
import {
  getWorkPeriodName,
  getEducationName,
} from "../../../utils/frontDataDict";

export default {
  // 组件名称
  name: "sameJobCom",
  // 组件参数 接收来自父组件的数据
  props: {
    industry: {
      type: String,
    },
  },
  components: {
    jobCard
  },
  // 组件状态值
  data() {
    return {
      dataList: [],
      swiper: null,
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    linkTo(id) {
      localStorage.setItem("currentJobId", id);
      this.$router.push({ path: `/jobDetail/${id}` });
    },
    init(jobInfo) {
      let params = {
        industry: [jobInfo.industry],
        page: 1,
        pageSize: 999,
      };
      getJobListOld(params).then((res) => {
        if (res && res.code === 0) {
          console.log("res>>>>", res);
          let data_list = res.data.list;
          data_list.forEach((v) => {
            try {
              if (Array.isArray(JSON.parse(v.education))) {
                v.education = getEducationName(v.education, true);
              }
            } catch {
              () => { };
            }
            try {
              if (Array.isArray(JSON.parse(v.industry))) {
                v.industry = JSON.parse(v.industry)[1];
                if (v.industry && v.industry.length > 3) {
                  v.industry = v.industry.slice(0, 3) + "...";
                }
              }

              if (Array.isArray(JSON.parse(v.city))) {
                v.city = JSON.parse(v.city)[1];
              }
              if (Number(v.workPeriod) != NaN) {
                v.workPeriod = getWorkPeriodName(v.workPeriod);
              }
            } catch {
              () => { };
            }
          });
          data_list = data_list.filter((item) => {
            return item.id !== jobInfo.id;
          });
          // 相似职位，需要先去重，再随机从大于5的数据里选择5条。
          if (data_list.length > 5) {
            data_list = this.getRandomArrayValue(data_list, 5);
          }
          console.log("data_list>>>", data_list);
          this.dataList = data_list;
          setTimeout(() => {
            this.initSwiper();
          });
        }
      });
    },
    getRandomArrayValue(arr, num) {
      var sData = arr.slice(0),
        i = arr.length,
        min = i - num,
        item,
        index;
      while (i-- > min) {
        index = Math.floor((i + 1) * Math.random());
        item = sData[index];
        sData[index] = sData[i];
        sData[i] = item;
      }
      return sData.slice(min);
    },
    slidePrev() {
      this.swiper.slidePrev();
    },
    slideNext() {
      console.log("this.swiper>>>", this.swiper);
      this.swiper.slideNext();
    },
    initSwiper() {
      if (this.swiper === null) {
        this.swiper = new Swiper("#sameJob", {
          slidesPerView: "auto",
          spaceBetween: 30,
        });
      } else {
        this.swiper.slideTo(0);
      }
    },
  },
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.t_itemBox {
  display: none;
}

.same-job-com {
  display: block;
  position: relative;
  width: 100%;
  padding-top: 20px;

  .title {
    width: 100%;
    height: 30px;
    font-size: 20px;
    font-family: MicrosoftYaHei;
    color: #000000;
    line-height: 30px;
    padding-left: 28px;
    margin-bottom: 20px;
    position: relative;

    &::after {
      content: "";
      width: 10px;
      height: 30px;
      position: absolute;
      left: 0;
      top: 0;
      background: #15389a;
    }
  }

  .itemBox {
    width: 100%;
    position: relative;

    .swiper-box {
      width: 100%;
      height: 220px;
      overflow: hidden;

      .swiper {
        width: 100%;
        height: 100%;

        .swiper-wrapper {
          .swiper-slide {
            width: 33.3%;
          }
        }
      }
    }

    .card-item {
      height: 220px;
      background: #ffffff;
      border-radius: 10px;
      border: 1px solid #d4d6da;
      position: relative;
      padding: 14px 14px;
      cursor: pointer;

      .line {
        width: 1px;
        height: 14px;
        opacity: 0.7;
        border: 1px solid #9ca8be;
        display: inline-block;
        margin: 0 18px;
      }

      .item-title {
        font-size: 22px;
        font-weight: 500;
        color: #37383d;
        max-width: 76%;
        line-height: 49px;
        margin-bottom: 8px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .item-price {
        font-size: 18px;
        color: #9597a0;
        line-height: 20px;
        margin-bottom: 20px;
      }

      .item-address {
        font-size: 17px;
        color: #9597a0;
        line-height: 17px;
        position: absolute;
        top: 30px;
        right: 18px;
      }

      .item-msg {
        .item-text {
          font-size: 15px;
          color: #9597a0;
          line-height: 16px;
        }
      }

      .item-bottom {
        margin-top: 20px;
        border-top: 0.5px dashed #9597a0;
        padding-top: 10px;
        width: 100%;
        display: flex;
        flex-flow: row nowrap;
        align-items: center;
        justify-content: flex-start;

        .bottom-img {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          border: 1px solid #d5d8db;
          margin-right: 14px;
        }

        .bottom-title {
          font-size: 18px;
          color: #37383d;
          line-height: 30px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .bottom-text {
          flex: 1;
          font-size: 16px;
          color: #9597a0;
          line-height: 28px;
          white-space: nowrap;
        }
      }
    }

    .steps {
      width: 40px;
      height: 40px;
      cursor: pointer;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      z-index: 2;

      img {
        width: 40px;
        height: 40px;
      }

      &.left {
        left: -27px;
      }

      &.right {
        right: -27px;
      }
    }
  }
}

@media screen and (max-width: 480px) {
  .itemBox {
    display: none;
  }
  .t_itemBox {
    display: block;
  }
}</style>
